﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;

using System.Threading;
using System.IO;

namespace LHS.Win
{
    public partial class frmRestore : Form
    {
        LHS.Core.Authority.AuthorityController objController = new LHS.Core.Authority.AuthorityController();
        // Methods
        public frmRestore()
        {
            this.InitializeComponent();
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void cmdCloseConnection_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;

            try
            {
                objController.CloseAllConnection(System.Configuration.ConfigurationManager.AppSettings["LoginNameDB"]);
            }
            catch (Exception ex)
            {
                Class.ShowException(ex);
            }
          
            MessageBox.Show("Tất cả các kết nối đều được đóng.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            this.cmdRestore.Enabled = true;
            this.Cursor = Cursors.Arrow;
        }

        private void cmdOpenFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.FilterIndex = 1;
            dialog.RestoreDirectory = true;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                this.txtFilePath.Text = dialog.FileName;
            }
        }

        private void cmdRestore_Click(object sender, EventArgs e)
        {
            if (!File.Exists(this.txtFilePath.Text))
            {
                MessageBox.Show("Xin vui lòng chọn file khôi phục", "Restore", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            else
            {
                base.ParentForm.Cursor = Cursors.AppStarting;
                this.th1 = new Thread(new ThreadStart(this.Thread1));
                this.th1.Start();
                this.Cursor = Cursors.WaitCursor;
            
                try
                {
                    objController.RestoreDatabase(System.Configuration.ConfigurationManager.AppSettings["DBName"].ToString(),this.txtFilePath.Text.Trim());
                    this.sp.EndDisplay();
                    this.th1 = null;
                    MessageBox.Show("Restore successful", "Restore", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    MessageBox.Show("Ứng dụng sẽ được đóng. Xin vui lòng mở lại", "Close", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    this.Cursor = Cursors.Arrow;
                    base.Close();
                    Application.Exit();
                }
                catch(Exception ex)
                {
                    this.sp.EndDisplay();
                    this.th1 = null;
                    Class.ShowException(ex);
                    this.Cursor = Cursors.Arrow;
                }
                base.ParentForm.Cursor = Cursors.Arrow;
            }
        }

        private void frmRestore_Load(object sender, EventArgs e)
        {
            this.cmdRestore.Enabled = false;
        }

        private void Thread1()
        {
            this.sp = new SplashScreen();
            this.sp.Show();
            this.sp.BeginDisplay();
        }

    }
}